package ar.Location.Map;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup.LayoutParams;
import ar.Location.PlaceObject.PlaceCategory;
import ar.Location.UI.PopUpMarkerView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class SharingLocations extends ItemizedOverlay<OverlayItem> {
	protected static final String TAG = "SharingLocations";
	public static ArrayList<MyOverlayItem> listSharingsOnMap = new ArrayList<MyOverlayItem>();
	public static ArrayList<MyOverlayItem> listSharingsOnCam = new ArrayList<MyOverlayItem>();

	GeoPoint center = null;
	Context ilContext = null;

	EnhancedMapView mapView = null;

	PopUpMarkerView popUpMarkerView = null;
	MapView.LayoutParams popUpMarkerLayoutParams = null;

	static int currentIndexTracking = -1;

	public SharingLocations(EnhancedMapView mMap, Context context,
			Drawable markerIcon) {
		super(markerIcon);

		initContext(mMap, context);

		setLastFocusedIndex(-1);
		populate();

		initPopUpMarker(mapView, ilContext);
	}

	public void initContext(EnhancedMapView mMap, Context context) {
		ilContext = context;
		mapView = mMap;
	}

	@Override
	public int size() {
		return listSharingsOnMap.size();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return listSharingsOnMap.get(i);
	}

	@Override
	protected boolean onTap(int index) {

		Log.d(TAG, "TAP TAP TAP!");
		currentIndexTracking = index;

		InterestingLocations.currentOverlayItem = listSharingsOnMap.get(index);
		PlaceCategory.currentPlace = null;

		showPopUpMarker(index);
		return super.onTap(index);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_UP) {
			popUpMarkerView.invisible();
			currentIndexTracking = -1;
		}

		return super.onTouchEvent(event, mapView);
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);
		if (currentIndexTracking != -1) {
			showPopUpMarker(currentIndexTracking);
		}
	}

	public void initPopUpMarker(EnhancedMapView eMapView, Context context) {

		popUpMarkerLayoutParams = new MapView.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, null,
				MapView.LayoutParams.BOTTOM_CENTER);
		popUpMarkerLayoutParams.mode = MapView.LayoutParams.MODE_MAP;

		popUpMarkerView = new PopUpMarkerView(eMapView, context);
		popUpMarkerView.setLayoutParams(popUpMarkerLayoutParams);
		popUpMarkerView.invisible();

		mapView.addView(popUpMarkerView, popUpMarkerLayoutParams);
	}

	public void showPopUpMarker(int overlayItemIndex) {
		ArrayList<MyOverlayItem> listMarkers = SharingLocations.listSharingsOnMap;
		if (overlayItemIndex >= 0 && overlayItemIndex < listMarkers.size()) {
			MyOverlayItem overlayItem = listMarkers.get(overlayItemIndex);
			if (overlayItem != null) {
				popUpMarkerLayoutParams.point = overlayItem.getPoint();
				popUpMarkerView.geoPoint = overlayItem.getPoint();
				popUpMarkerView.updateContent(overlayItem.getTitle(),
						overlayItem.getSnippet());
				popUpMarkerView.visible();
			}
		}
	}

	public void refreshListMarkers() {
		setLastFocusedIndex(-1);
		populate();
	}

	public void addMarker(MyOverlayItem myOverlayItem) {
		// TODO Auto-generated method stub
		listSharingsOnMap.add(myOverlayItem);
		setLastFocusedIndex(-1);
		populate();
	}

	public void clearMarker() {
		// TODO Auto-generated method stub

		if (!listSharingsOnMap.isEmpty()) {
			setLastFocusedIndex(-1);
			populate();

			listSharingsOnMap.clear();

			setLastFocusedIndex(-1);
			populate();

			popUpMarkerView.invisible();
		}
	}
}
